$(function () {
});

var storepurchaseapplyAuditVm = new Vue({
	el:'#storepurchaseapplyAuditPage',
	data:{
        purchaseapplyAuditPageVisible: false,
        purchaseApplyForm:[],
        e: {
            materialUnitEnum : [],
            materialSupplier: [], /*采购供应商*/
            taxEnum:[{value: 1,label: '含税'},{value: 0,label: '不含税'}],
        },
        purchaseApplyForm: {
            storePurchaseApplyEntity:{},
            storePurchaseApplyDetailEntityList: []
        },
	},
	methods: {
        loadData: function (data) {
            this.purchaseapplyAuditPageVisible = true;
            this.e.materialUnitEnum = data.materialUnitEnum;
            this.purchaseApplyForm.storePurchaseApplyEntity = data.storePurchaseApply;
            this.e.materialSupplier = data.materialSupplier;

            // 获取采购明细
            $.get(baseURL + "purchase/storepurchaseapplydetail/queryList/"+data.storePurchaseApply.applyId, function(r){
                console.log(r);
                storepurchaseapplyAuditVm.purchaseApplyForm.storePurchaseApplyDetailEntityList = r;
            });
        },
        /*格式化 单位*/
        formatUnit: function (row, column, cellValue, index) {
            for(var i=0; i<this.e.materialUnitEnum.length; i++){
                if(cellValue == this.e.materialUnitEnum[i].id){
                    return this.e.materialUnitEnum[i].describe
                }
            }
        },
        onSubmit: function (event) {
            $.ajax({
                type: "POST",
                url: baseURL + "purchase/storepurchaseapply/audit",
                contentType: "application/json",
                data: JSON.stringify(this.purchaseApplyForm),
                success: function(r){
                    if(r.code === 0){
                        alert('操作成功', function(index){
                            storepurchaseapplyAuditVm.purchaseapplyAuditPageVisible = false;
                            vm.reload();
                        });
                    }else{
                        alert(r.msg);
                    }
                }
            });
        },
		reject:function (event) {
			$.ajax({
				type: "POST",
				url: baseURL + "purchase/storepurchaseapply/reject",
				contentType: "application/json",
				data: JSON.stringify(this.purchaseApplyForm),
				success: function(r){
					if(r.code === 0){
						alert('驳回成功', function(index){
							storepurchaseapplyAuditVm.purchaseapplyAuditPageVisible = false;
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
        /*单个耗材，采购总价修改事件*/
        totalMoneyChange: function (row,index) {
            var storePurchaseApplyDetail = this.purchaseApplyForm.storePurchaseApplyDetailEntityList[index];
            storePurchaseApplyDetail.unitMoney = percentNum(storePurchaseApplyDetail.totalMoney,storePurchaseApplyDetail.materialQuantity)
            this.purchaseApplyForm.storePurchaseApplyDetailEntityList.splice(index,1,storePurchaseApplyDetail);

            // 修改采购总价
            this.changePurchaseTotal();
        },
        // 修改采购总价
        changePurchaseTotal: function () {
            storepurchaseapplyAuditVm.purchaseApplyForm.storePurchaseApplyEntity.totalMoney = 0
            for(var i=0; i<this.purchaseApplyForm.storePurchaseApplyDetailEntityList.length; i++){
                storepurchaseapplyAuditVm.purchaseApplyForm.storePurchaseApplyEntity.totalMoney
                = Number(storepurchaseapplyAuditVm.purchaseApplyForm.storePurchaseApplyEntity.totalMoney)
                    + Number(storepurchaseapplyAuditVm.purchaseApplyForm.storePurchaseApplyDetailEntityList[i].totalMoney);
            }
        },
	}
});

function percentNum(num,num2) {
    return (Math.round(num / num2 * 100) / 100.00);
}